package edu.ciee.tz.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import edu.ciee.tz.dao.GouruDAO;
import edu.ciee.tz.dao.GouruEbDAO;
import edu.ciee.tz.model.Gouru;
import edu.ciee.tz.model.GouruEb;
import edu.ciee.tz.model.Laiyuan;
import edu.ciee.tz.model.Yuangong;

public class GouruAction {
	private int id;
	private String gouru_time;
	private int laiyuan;
	private int fz_person;
	
	private int pici_id;
	private String eb_id;
	private int gouru_id;
	private GouruEbDAO gouruEbDAO;
	
	private String tip;
	private List resultList;
	private List ebList;
	private GouruDAO gouruDAO;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getGouru_time() {
		return gouru_time;
	}
	public void setGouru_time(String gouru_time) {
		this.gouru_time = gouru_time;
	}
	public int getLaiyuan() {
		return laiyuan;
	}
	public void setLaiyuan(int laiyuan) {
		this.laiyuan = laiyuan;
	}
	public int getFz_person() {
		return fz_person;
	}
	public void setFz_person(int fz_person) {
		this.fz_person = fz_person;
	}
	public String getTip() {
		return tip;
	}
	public void setTip(String tip) {
		this.tip = tip;
	}
	public List getResultList() {
		return resultList;
	}
	public void setResultList(List resultList) {
		this.resultList = resultList;
	}
	
	public List getEbList() {
		return ebList;
	}
	public void setEbList(List ebList) {
		this.ebList = ebList;
	}
	public GouruDAO getGouruDAO() {
		return gouruDAO;
	}
	@Resource
	public void setGouruDAO(GouruDAO gouruDAO) {
		this.gouruDAO = gouruDAO;
	}
	
	public int getPici_id() {
		return pici_id;
	}
	public void setPici_id(int pici_id) {
		this.pici_id = pici_id;
	}
	public String getEb_id() {
		return eb_id;
	}
	public void setEb_id(String eb_id) {
		this.eb_id = eb_id;
	}
	public int getGouru_id() {
		return gouru_id;
	}
	public void setGouru_id(int gouru_id) {
		this.gouru_id = gouru_id;
	}
	public GouruEbDAO getGouruEbDAO() {
		return gouruEbDAO;
	}
	@Resource
	public void setGouruEbDAO(GouruEbDAO gouruEbDAO) {
		this.gouruEbDAO = gouruEbDAO;
	}
	@SuppressWarnings("unchecked")
	public String forLuru(){
			return "success";			
	}
	public String insert(){
		HttpServletRequest request = ServletActionContext.getRequest();
		ActionContext ctx = ActionContext.getContext();
		Map m = ctx.getParameters();
		String[] eb = request.getParameterValues("eb");
		
		try{
			Gouru gr=new Gouru();
			gr.setGouru_time(this.getGouru_time());
			
			Laiyuan ly=new Laiyuan();
			ly.setId(this.getLaiyuan());
			gr.setLaiyuan(ly);
			
			Yuangong yg=new Yuangong();
			yg.setId(this.getFz_person());
			gr.setFz_person(yg);
			this.getGouruDAO().insert(gr);
			
			for(int i=0;i<eb.length;i++){
				GouruEb gouruEb=new GouruEb();
				gouruEb.setEb_id(eb[i]);
				gr.setId(this.getGouruDAO().getAuto_increment());
				gouruEb.setGouru_id(gr);
				this.getGouruEbDAO().insert(gouruEb);
			}
						
			return "success";
		}catch(Exception e){
			this.setTip(e.toString());
			return "error";
		}
	}
	public String delete(){
		try{
			Gouru gr=new Gouru();
			gr.setGouru_time(this.getGouru_time());
			gr.setId(this.getId());
			Laiyuan ly=new Laiyuan();
			ly.setId(this.getLaiyuan());
			gr.setLaiyuan(ly);
			
			Yuangong yg=new Yuangong();
			yg.setId(this.getFz_person());
			gr.setFz_person(yg);
			
			this.getGouruDAO().delete(gr);
			
			GouruEb gouruEb=new GouruEb();
			gouruEb.setId(this.getPici_id());
			gouruEb.setEb_id(this.getEb_id());
			gouruEb.setGouru_id(gr);
			this.getGouruEbDAO().delete(gouruEb);
			
			return "refresh";
		}catch(Exception e){
			this.setTip(e.toString());
			return "error";
		}
	}
	public String forEdit(){
		return "success";
	}
	public String edit(){
		try{
			Gouru gr=new Gouru();
			gr.setGouru_time(this.getGouru_time());
			gr.setId(this.getId());
			Laiyuan ly=new Laiyuan();
			ly.setId(this.getLaiyuan());
			gr.setLaiyuan(ly);
			
			Yuangong yg=new Yuangong();
			yg.setId(this.getFz_person());
			gr.setFz_person(yg);
			
			this.getGouruDAO().update(gr);
			
			GouruEb gouruEb=new GouruEb();
			gouruEb.setId(this.getPici_id());
			gouruEb.setEb_id(this.getEb_id());
			gouruEb.setGouru_id(gr);
			this.getGouruEbDAO().update(gouruEb);
			
			return "refresh";
		}catch(Exception e){
			this.setTip(e.toString());
			return "error";
		}
	}
	public String search(){
		try{
			Gouru gr=new Gouru();
			gr.setGouru_time(this.getGouru_time());
			gr.setId(this.getId());
			Laiyuan ly=new Laiyuan();
			ly.setId(this.getLaiyuan());
			gr.setLaiyuan(ly);
			
			Yuangong yg=new Yuangong();
			yg.setId(this.getFz_person());
			gr.setFz_person(yg);
			
			GouruEb gouruEb=new GouruEb();
			gouruEb.setId(this.getPici_id());
			gouruEb.setEb_id(this.getEb_id());
			gouruEb.setGouru_id(gr);
			
			List l1=this.getGouruDAO().search(gr);
			List l2=this.getGouruEbDAO().search(gouruEb);
			
			
			
			this.setResultList(this.getGouruDAO().search(gr));
			this.setEbList(this.getGouruEbDAO().search(gouruEb));
			return "success";
		}catch(Exception e){
			this.setTip(e.toString());
			return "error";
		}
	}
}
